#include "lightpoint.h"

LightPoint::LightPoint(QWidget *parent) : QWidget(parent)
{
    step = 10;
    interval = 100;
    bgColor = QColor(255, 0, 0);
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(100);

    offset = 0;
    add = true;
}

void LightPoint::setStep(int step)
{
    if(step!= this->step)
    {
        this->step = step;
        this->update();
    }
}

void LightPoint::setInterval(int interval)
{
    if(this->interval != interval)
    {
        this->interval = interval;
        timer->setInterval(interval);
        this->update();
    }
}

void LightPoint::setBgColor(const QColor &bgColor)
{
    if(this->bgColor != bgColor)
    {
        this->bgColor = bgColor;
        this->update();
    }
}

void LightPoint::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    int width = this->width();
    int height = this->height();
    int side = qMin(width, height);

    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(width/2, height/2);
    painter.scale(side/200.0, side/200.0);

    drawBg(&painter);
}

void LightPoint::drawBg(QPainter *painter)
{
    int radius = 99;
    painter->save();

    QRadialGradient g(QPoint(0, 0), radius);

    (offset < 70 && add) ? (offset += step) : (add = false);
    (offset > 0 && !add) ? (offset -= step) : (add = true);

    bgColor.setAlpha(255);
    g.setColorAt(0.1, bgColor);
    bgColor.setAlpha(100 + offset);
    g.setColorAt(0.3, bgColor);
    bgColor.setAlpha(50 + offset);
    g.setColorAt(0.6, bgColor);
    bgColor.setAlpha(0);
    g.setColorAt(1.0, bgColor);

    painter->setPen(Qt::NoPen);
    painter->setBrush(g);
    painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);

    painter->restore();
}



